package cn.fengliu.controller;

import cn.fengliu.Dao.LargeFileDao;
import cn.fengliu.Dao.UserDao;
import cn.fengliu.entity.LargeFile;
import cn.fengliu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/mark5")
public class LoginController {
    @Autowired
    UserDao userDao;
    @Autowired
    LargeFileDao largeFileDao;

    @RequestMapping("/go2login")
    public String login(Model model){

        return "/login";
    }
    @RequestMapping ("/login")
    public String login(HttpSession session, Model model, String phone, String pwd, HttpServletRequest request){

        session.removeAttribute("info");
        System.out.println(phone);
        System.out.println(pwd);
        User user = userDao.findByPwd(phone);

        if (user!=null){
      if (user.getPwd().equals(pwd)){
          System.out.println(user);
//          反止  瞎搞  不获取他的密码
          user.setPwd(" ");
          session.setAttribute("user",user);

          return "forward:/mark5/go2login";
      } }
      model.addAttribute("err","登录: 手机号 或 密码错误");
        return "forward:/mark5/go2login";

    }
    @GetMapping("/showimg/{pic}")
    public void showImg(@PathVariable("pic") String id ,
                        HttpServletResponse resp)
            throws IOException {
        System.out.println("===============");
        System.out.println("pic="+id);
        LargeFile one=largeFileDao.findOne(id);
        Object content=one.getContent();
        OutputStream os=resp.getOutputStream();
        // 告诉 文件 我读的是 jpg的格式
        resp.setContentType("image/jpeg");
        byte[] buf=(byte[])content;
        os.write(buf);
        os.flush();
    }

    @RequestMapping("/go2updpwd")
    public String go2updpwd(Model model){
        return "/updpwd";
    }


    @RequestMapping ("/updpwd")
    public String updpwd(HttpSession session, Model model, String phone, String pwd ,String pwd2){
        System.out.println(phone);
        System.out.println(pwd);
        System.out.println(pwd2);
//        User user = (User) session.getAttribute("user");
           User user = userDao.findByPwd(phone);

           System.out.println(user);
            if (!pwd.equals(pwd2)) {
                model.addAttribute("err", "两次密码不一样");
                return "forward:/mark5/go2updpwd";}
            if (user.getPwd().equals(pwd)) {
                    System.out.println(user);
                    model.addAttribute("err", "新密码与旧密码一致");
                    return "forward:/mark5/go2updpwd"; }
                    user.setPwd(pwd);
            int updatePwd = userDao.updatePwd(user);
            model.addAttribute("info","修改成功");
            return "forward:/mark5/logout.do";
    }
    @RequestMapping("/logout.do")
    public  String logout(HttpSession session,Model model){
        System.out.println("销毁数据");
        session.invalidate();
        return "forward:/mark5/go2login";
    }
}
